<?php
namespace app\index\controller;
use app\common\exception\System as SystemException;
use app\index\model\Video as Videos;
use app\index\model\Imgtext as News;
use app\index\model\Comment as Newscomment;
use app\index\model\Videocomment;
use app\index\model\Comment;
use app\index\model\Newsfollow;
use think\Db;

class Communal extends Common
{
    /**
   * 不需要登录
   * @var array
   */
  protected $noNeedLogin = ['*'];

  /**
         * 关注文章
         *
         */
    public function addfollow(){
      $Newsfollow=new Newsfollow;
      $param['follow_member_id'] = $this->request->request("follow_member_id");
      $param['member_id']=$this->auth->member_id;
      
      $res=Newsfollow::where([
        ['follow_member_id',"=",$param['follow_member_id']],
        ['member_id',"=",$param['member_id']]
        ])->find();
      if($res){
      $result=Newsfollow::where($param)->delete();
      if($result){
        $arr=['status'=>1,'msg'=>'取消关注'];
          return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
      }else{
        $arr=['status'=>3,'msg'=>'取消失败'];
          return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
      }
    }else{
      $result=$Newsfollow->allowField(true)->save($param);
      if($result){
        $arr=['status'=>2,'msg'=>'关注成功'];
          return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
      }else{
        $arr=['status'=>4,'msg'=>'关注失败'];
          return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
      }
    }
    }
  public function praise(){
  	$data=$this->request->param();
  	$data['member_id']=$this->auth->member_id;
    $res=db("news_praise")->where($data)->find(); 
    if($res){
    	$result=db("news_praise")->where($data)->delete();
    	if($result){
    		$arr=['status'=>1,'msg'=>'取消点赞'];
      		return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}else{
		  	$arr=['status'=>3,'msg'=>'取消失败'];
	      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}
    }else{
    	$data['create_time']=time();
    	$result=db("news_praise")->insert($data);
    	if($result){
    		$arr=['status'=>2,'msg'=>'点赞成功'];
      		return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}else{
		  	$arr=['status'=>4,'msg'=>'点赞失败'];
	      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}
    }
  }
  public function Collect(){
  	$data=$this->request->param();
  	$data['member_id']=$this->auth->member_id;
    $res=db("news_collect")->where($data)->find(); 
    if($res){
    	$result=db("news_collect")->where($data)->delete();
    	if($result){
    		$arr=['status'=>1,'msg'=>'取消收藏'];
      		return $this->successReturn($arr,SystemException::E_SUCCESS,$arr['msg']); 
	  	}else{
		  	$arr=['status'=>3,'msg'=>'取消失败'];
	      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}
    }else{
    	$data['create_time']=time();
    	$result=db("news_collect")->insert($data);
    	if($result){
    		$arr=['status'=>2,'msg'=>'收藏成功'];
      		return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}else{
		  	$arr=['status'=>4,'msg'=>'收藏失败'];
	      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}
    }
  }
  public function comments(){
  	$data=$this->request->param();
  	// $data['member_id']=$this->auth->member_id;
  	$data['member_id']=2;
  	$Comment=new Comment;
  	$res=$Comment->allowField(true)->save($data);
  	if($res){
    		$arr=['status'=>1,'msg'=>'评论成功'];
      		return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}else{
		  	$arr=['status'=>0,'msg'=>'评论失败'];
	      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}
  }
}
